package com.open.rbac.constant;

import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

import com.open.center.common.constant.Enumerator;
import com.open.center.common.databind.JsonEnumDeserializer;
import com.open.center.jpa.converter.DbEnumConverter;

/**
 * 状态枚举
 *
 * @author Riche's
 * @since 2025/6/7
 */
@Getter
public enum StatusEnum implements Enumerator<StatusEnum, Integer> {

    VALID(1, "有效"),

    INVALID(2, "无效");

    private final Integer id;

    private final String name;

    StatusEnum(int id, String name) {
        this.id = id;
        this.name = name;
    }

    /**
     * {@link JsonEnumDeserializer }
     *
     * 枚举基类 {@link Enumerator } 全局的反序列化
     *
     *  {@JsonCreator 注解将不再生效 }
     *
     *  { @RequestBody 参数的枚举属性的反序列化 }
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static StatusEnum as(@JsonProperty("id") int id) {
        return Arrays.stream(StatusEnum.values())
                .filter(e -> e.getId().equals(id))
                .findAny()
                .orElse(null);
    }

    public static class Converter extends DbEnumConverter<StatusEnum, Integer> {
        public Converter() {
            super(StatusEnum.class);
        }
    }


}
